//
// Created by 原帅 on 2022/9/28.
//

#include "RInitializer.h"
#include "RLog.h"
#include "RInitProcIF.h"
#include "algorithm"

int RInitializer::startInitialize(int argc,char** argv) {
    RLogDbg("start inializing...")
    if (this->initInterFaces.empty())
    {
        RLogDbg("initialize vector is empty! nothing to do!")
    }
    else
    {
        for (auto interface : this->initInterFaces) {
            if (interface == nullptr)
            {
                RLogErr("an interface is nullptr in initialize vector,auto remove it")
                this->initInterFaces.erase(std::find(initInterFaces.begin(),initInterFaces.end(),interface));
                continue;
            }
            else
            {
                int iRet = interface->initProcess(argc, argv);
                if (0 != iRet) {
                    return interface->initFail(iRet);
                }
            }
        }
    }
    RLogDbg("inialization done!")
    return 0;
}

int RInitializer::registerInitProc(RInitProcIF *rInitProcIf) {
    if (std::find(initInterFaces.begin(),initInterFaces.end(),rInitProcIf)==initInterFaces.end())
    {
        initInterFaces.push_back(rInitProcIf);
    }
    else
    {
        RLogErr("there is a same interface already in initializer vector");
        return -1;
    }
    return 0;
}
